import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;

/**
 * 对Main的改进
 */
public class ImproveMain {

    private static ExecutorService executor = Executors.newFixedThreadPool(100);//返回固定线程数的executor

    public static void main(String[] args) throws IOException {



        ServerSocket serverSocket = new ServerSocket(80);

        while (!executor.isShutdown()){
            Socket socket = serverSocket.accept();//阻塞等待
            try {
                Runnable task = new Runnable() {
                    @Override
                    public void run() {
                        handleRequest(socket);
                    }
                };

                //交给executor执行即可
                executor.execute(task);
            } catch (RejectedExecutionException e) {
                if (!executor.isShutdown()){
                    System.out.println("接受任务被拒绝");
                    throw e;
                }
            }
        }
    }

    public void stop(){
        //等待线程执行完成后关闭线程池
        executor.shutdown();
    }
    private static void handleRequest(Socket socket) {
    }
}